//
//  UIButtonExtension.swift
//  DemoNavBar
//
//  Created by YangHuan on 2019/12/13.
//  Copyright © 2019 YangHuan. All rights reserved.
//

import UIKit

enum ButtonEdgeInsetsStyle {
    case top // image在上，label在下
    case left  // image在左，label在右
    case bottom  // image在下，label在上
    case right // image在右，label在左
}

extension UIButton {
    func layoutButtonEdgeInsets(style:ButtonEdgeInsetsStyle,space:CGFloat) {
        var labelWidth : CGFloat = 0.0
        var labelHeight : CGFloat = 0.0
        var imageEdgeInset = UIEdgeInsets.zero
        var labelEdgeInset = UIEdgeInsets.zero
        let imageWith = self.imageView?.frame.size.width
        let imageHeight = self.imageView?.frame.size.height
        labelWidth = (self.titleLabel?.intrinsicContentSize.width) ?? 0
        labelHeight = (self.titleLabel?.intrinsicContentSize.height) ?? 0
        switch style {
        case .top:
            imageEdgeInset = UIEdgeInsets(top: -labelHeight-space/2.0, left: 0, bottom: 0, right: -labelWidth)
            labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!, bottom: -imageHeight!-space/2.0, right: 0)
        case .left:
            imageEdgeInset = UIEdgeInsets(top: 0, left: -space/2.0, bottom: 0, right: space/2.0);
            labelEdgeInset = UIEdgeInsets(top: 0, left: space/2.0, bottom: 0, right: -space/2.0);
        case .bottom:
            imageEdgeInset = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight-space/2.0, right: -labelWidth)
            labelEdgeInset = UIEdgeInsets(top: -imageHeight!-space/2.0, left: -imageWith!, bottom: 0, right: 0)
        case .right:
            // To Do print("坐标是====\(labelWidth)=====\(space)")
            imageEdgeInset = UIEdgeInsets(top: 0, left: labelWidth+space/2.0, bottom: 0, right: -labelWidth-space/2.0)
            labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!-space/2.0, bottom: 0, right: imageWith!+space/2.0)
        }
        self.titleEdgeInsets = labelEdgeInset
        self.imageEdgeInsets = imageEdgeInset
    }
}


// MARK: - 仿支付宝蚂蚁森林动画
public extension UIButton {
    func startAnimation() {
        let time = random(min: 4, max: 8)
        let offsetY = random(min: 5, max: 15)
        let ani = CAKeyframeAnimation(keyPath: "position")
        ani.isRemovedOnCompletion = false
        ani.fillMode = CAMediaTimingFillMode.forwards
        ani.values = [self.center, CGPoint(x: self.centerX, y: self.centerY + CGFloat(offsetY)), self.center]
        ani.repeatCount = Float(Int.max)
        ani.duration = CFTimeInterval(time)
        ani.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
        self.layer.add(ani, forKey: "PostionAni")
    }
    
    func move(to point: CGPoint?) {
        guard let po = point else {return}
        let movePath = UIBezierPath()
        movePath.move(to: self.center)
        movePath.addLine(to: po)
        let ani = CAKeyframeAnimation(keyPath: "position")
        ani.path = movePath.cgPath
        ani.duration = 0.5
        ani.repeatCount = 1.0
        ani.calculationMode = CAAnimationCalculationMode.paced
        ani.isRemovedOnCompletion = false
        ani.fillMode = CAMediaTimingFillMode.forwards
        ani.delegate = self
        self.layer.add(ani, forKey: "postionAni")
    }
    
    private func random(min: Int, max: Int) -> Int {
        if min >= max {
            return min
        }
        return (Int(arc4random_uniform(UInt32(max - min)) + UInt32(min)))
    }
}

extension UIButton: CAAnimationDelegate {
    public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        self.layer.removeAllAnimations()
        self.isHidden = true
        self.removeFromSuperview()
    }
}



extension UIButton {
    
    public func addBackgroundImage() {
        self.setBackgroundImage(UIImage(named: "btnNormal"), for: .normal)
        self.setBackgroundImage(UIImage(named: "btnNormal"), for: .highlighted)
    }


}





